package mobweb.booktrack;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

@SuppressLint("DefaultLocale")
public class SearchActivity extends Activity implements
AsyncTaskCompleteListener<Book> {
	private ProgressDialog progressDialog;

	EditText searchedText;
	String searchedTxt, searchedCriteria;
	int flag, idx;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		Button searchButton = (Button) findViewById(R.id.searchButton);
		searchButton.setOnClickListener(new OnClickListener() {

			@SuppressLint("DefaultLocale")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				searchedText = (EditText) findViewById(R.id.searchedText);
				searchedTxt = searchedText.getText().toString();
				RadioGroup choice = (RadioGroup) findViewById(R.id.searchRadioGroup);

				flag = choice.getCheckedRadioButtonId();
				View radioButton = choice.findViewById(flag);
				idx = choice.indexOfChild(radioButton);
				RadioButton x = (RadioButton) findViewById(flag);

				switch (idx) {
				case 0:
					searchedCriteria = "intitle:";
					break;
				case 1:
					searchedCriteria = "inauthor:";
					break;
				case 2:
					searchedCriteria = "isbn:";
					break;
				default:
					Toast.makeText(getBaseContext(),
							" I can\'t read the choice!", Toast.LENGTH_LONG)
							.show();
					break;
				}

				searchBook(searchedCriteria, convertString(searchedTxt)
						.toLowerCase());
			}
		});
	}

	/**
	 * searching data with criteria and values. it will returns us the first
	 * data found
	 * 
	 * @param criteria
	 * @param value
	 */
	@SuppressLint("DefaultLocale")
	private void searchBook(String criteria, String value) {
		String url = "https://www.googleapis.com/books/v1/volumes?q="
				+ criteria + value;
		Toast.makeText(getBaseContext(), url, Toast.LENGTH_LONG).show();
		Context context = getApplicationContext();

		NetworkTask networkTask = new NetworkTask(context, this);
		networkTask.execute(url);
		progressDialog = ProgressDialog.show(this, "", "Searching for data...");

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_search, menu);
		return true;
	}

	@Override
	public void onTaskComplete(Book result) {
		// TODO Auto-generated method stub

		if (progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
		Intent i = new Intent(getApplicationContext(), BookActivity.class);
		i.putExtra("book", result);
		startActivity(i);

	}

	protected String convertString(String incoming) {
		String urlString = "";
		String delims = "[ ]+";
		String[] tokens = incoming.split(delims);
		for (int i = 0; i < tokens.length; i++) {
			urlString += tokens[i];
			urlString += "+";
		}

		return urlString.substring(0, urlString.length() - 1);
	}
}
